// src/api/alovaInstance.js
import { createAlova } from "alova";
import fetchAdapter from "alova/fetch";
import vueHook from "alova/vue";
import { getToken } from "@/utils/auth";
import { Encapsulation, newLoading } from "./encapsulation";
export const createSharedAlovaInstance = (timeout,url) => {
  return createAlova({
    baseURL: `${import.meta.env.VITE_APP_BASE_API}` + url,
    statesHook: vueHook,
    timeout: timeout,
    cacheFor: null,
    requestAdapter: fetchAdapter(),
    beforeRequest({ config }) {
      newLoading(true);
      if (getToken()) {
        config.headers["Authorization"] = "Bearer " + getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
        // config.headers["content-type"] ='application/json;charset=UTF-8;'
      }
      return config;
    },
    responded: {
      onSuccess: (response, method) => {
        if (response.status >= 400) {
          newLoading(false);
          throw new Error(response.statusText);
        }
        return Encapsulation(response);
      },
      onError: (err, method) => {
        newLoading(false);
      },
    },
  });
};
